package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonResult;

import com.imti.mall.model.cms.pojo.CmsPrefrenceAreaProductRelation;
import com.imti.mall.model.cms.pojo.CmsSubjectProductRelation;
import com.imti.mall.model.pms.pojo.*;
import com.imti.mall.model.pms.vo.PmsProductParam;
import com.imti.mall.model.pms.vo.PmsProductVo;
import com.imti.mall.product.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/v1/product")
@Api(tags = "商品信息列表")
public class ProductController {

    @Autowired
    ProductService productService;
    @Autowired
    PmsProductCategoryService pmsProductCategoryService;
    @Autowired
    PmsProductAttributeValueService pmsProductAttributeValueService;
    @Autowired
    PmsMemberPriceService pmsMemberPriceService;
    @Autowired
    PmsProductFullReductionService pmsProductFullReductionService;
    @Autowired
    PmsProductLadderService pmsProductLadderService;
    @Autowired
    PmsSkuStockService pmsSkuStockService;
    @Autowired
    CmsSubjectProductRelationService cmsSubjectProductRelationService;
    @Autowired
    CmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;

    /**
     * 查询商品全部信息
     * @return
     */
    @GetMapping("/findproduct")
    @ApiOperation(value = "查询商品全部信息app")
    public CommonResult findProduct(PmsProduct pmsProduct,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> map =new HashMap<>();
        map.put("pmsProduct",pmsProduct);
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        List<PmsProduct> productList = productService.findPmsProduct(map);
        int total =productService.countPmsProduct(pmsProduct);
        Map<String, Object> data = new HashMap<>();
        data.put("productList",productList);
        data.put("total",total);
        return CommonResult.success(data);
    }


    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id商品属性值表app传id skuStockList=Sku信息 PmsProduct=商品的信息 ")
    @GetMapping("/getProductApp/{id}")
    public CommonResult getProductAppById(@PathVariable("id") Integer id){
        PmsProduct pmsProductById = productService.getPmsProductById(id);
        Map<String,Object> map=new HashMap<>();
        if(pmsProductById!=null){

        map.put("PmsProduct",pmsProductById);
        List<PmsSkuStock> skuStockList=pmsSkuStockService.getPmsSkuStockproductProductId(pmsProductById.getId().intValue());
        map.put("skuStockList",skuStockList);
        return CommonResult.success(map);
        }else {
            return CommonResult.failed("请输入正确编号字段");
        }
    }

    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id商品属性值表")
    @GetMapping("/getproduct/{id}")
    public CommonResult getProductById(@PathVariable("id") Integer id){
        PmsProduct pmsProductById = productService.getPmsProductById(id);
        PmsProductCategory pmsProductCategory =pmsProductCategoryService.getPmsProductCategoryById(pmsProductById.getProductCategoryId().intValue());
        PmsProductAttributeValue pmsProductAttributeValue=new PmsProductAttributeValue();
        //商品属性值表返回商品属性相关{productAttributeId: 0, value: ''}
        pmsProductAttributeValue.setProductId(pmsProductById.getId());
        List<PmsProductAttributeValue> pmsProductAttributeValueList= pmsProductAttributeValueService.findPmsProductAttributeValue(pmsProductAttributeValue);
        //商品会员价格{memberLevelId: 0,memberPrice: 0,memberLevelName: null}list
        List<PmsMemberPrice> memberPriceList=pmsMemberPriceService.getPmsMemberPriceId(pmsProductById.getId().intValue());
        //商品满减
        //    productFullReductionList: [{fullPrice: 0, reducePrice: 0}],
        List<PmsProductFullReduction> productFullReductionList=pmsProductFullReductionService.getPmsProductFullReductionId(pmsProductById.getId().intValue());
        //商品阶梯价格
       // productLadderList: [{count: 0,discount: 0,price: 0}],
        List<PmsProductLadder> productLadderList =pmsProductLadderService.getPmsProductLadderProductId(pmsProductById.getId().intValue());
        //商品sku库存信息{lowStock: 0, pic: '', price: 0, sale: 0, skuCode: '', spData: '', stock: 0}
        //skuStockList: [],
        List<PmsSkuStock> skuStockList=pmsSkuStockService.getPmsSkuStockproductProductId(pmsProductById.getId().intValue());
        //    商品相关专题{subjectId: 0}
        //    subjectProductRelationList: []
        List<CmsSubjectProductRelation> subjectProductRelationList  = cmsSubjectProductRelationService.getCmsSubjectProductRelationProductId(pmsProductById.getId().intValue());
        // 商品相关优选{prefrenceAreaId: 0}
        //    prefrenceAreaProductRelationList: [], //商品相关优选{prefrenceAreaId: 0}
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList  = cmsPrefrenceAreaProductRelationService.getCmsPrefrenceAreaProductRelationProductId(pmsProductById.getId().intValue());

        Map<String,Object> map=new HashMap<>();
        map.put("PmsProduct",pmsProductById);
        map.put("cateParentId",pmsProductCategory.getParentId());
        map.put("productAttributeValueList",pmsProductAttributeValueList);
        map.put("memberPriceList",memberPriceList);
        map.put("productFullReductionList",productFullReductionList);
        map.put("productLadderList",productLadderList);
        map.put("skuStockList",skuStockList);
        map.put("subjectProductRelationList",subjectProductRelationList);
        map.put("prefrenceAreaProductRelationList",prefrenceAreaProductRelationList);
        return CommonResult.success(map);
    }

    /**
     * 修改商品信息updategetPmsProductById
     * @param productParam
     * @return
     */
    @PostMapping ("/updategetPmsProductById")
    public CommonResult updategetPmsProductById(@RequestBody  PmsProductParam productParam) {
        int count= productService.updategetPmsProductById(productParam);
        if(count>0){
            return CommonResult.success("修改成功");
        }else{
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 根据id删除商品信息
     * @param
     * @return
     */
    @PostMapping ("/delproduct")
    public CommonResult delProductById(PmsProduct pmsProduct){
        System.out.println("======================="+pmsProduct);
       if (productService.delPmsProductById(pmsProduct)>0){
           return CommonResult.success("删除成功");
       }else {
           return CommonResult.failed("删除失败");
       }

    }

    /**
     * 根据id上架下架修改
     * @paramupdatePmsProductPublishStatusById
     * @return
     */
    @PostMapping("/updatePmsProductPublishStatusById")
    public CommonResult updatePmsProductPublishStatusById(PmsProductVo pmsProduct){
        Long[] ids = pmsProduct.getIds();
        Integer integer=0;
        for (int i=0;i<ids.length;i++){
            integer=0;
            pmsProduct.setId(ids[i]);
            integer = productService.updatePmsProductPublishStatusById(pmsProduct);
        }

        if (integer>0){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }


    /**
     * 根据id新品不是新品修改
     * @param
     * @return
     */
    @RequestMapping("/updatePmsProductNewStatusById")
    public CommonResult updatePmsProductNewStatusById(PmsProductVo pmsProduct){
        Long[] ids = pmsProduct.getIds();
        Integer integer=0;
        for (int i=0;i<ids.length;i++){
            integer=0;
            pmsProduct.setId(ids[i]);
            integer = productService.updatePmsProductNewStatusById(pmsProduct);

        }
        if (integer>0){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }


    }


    /**
     * 推荐 不推荐修改
     * @param
     * @return
     */
    @ApiOperation(value = "推荐 不推荐修改")
    @RequestMapping("/updatePmsProductRecommandStatusById")
    public CommonResult updatePmsProductRecommandStatusById(PmsProductVo pmsProduct){
        Long[] ids = pmsProduct.getIds();
        Integer integer=0;
        for (int i=0;i<ids.length;i++){
            integer=0;
            pmsProduct.setId(ids[i]);
            integer = productService.updatePmsProductRecommandStatusById(pmsProduct);
        }
        if (integer>0){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }


    }


    /**
     * 添加商品信息
     * @param productParam
     * @return
     */
    @ApiOperation(value = "添加商品信息")
    @PostMapping("addPmsProduct")
    public CommonResult addPmsProduct(@RequestBody PmsProductParam productParam){
       int count =productService.addPmsProduct(productParam);
       if(count>0){
           return CommonResult.success("添加成功");
       }else{
           return CommonResult.failed("添加失败");
       }

    }



    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/getProductToSale/{id}")
    public PmsProduct getProductToSale(@PathVariable("id") Integer id){
        PmsProduct pmsProduct = productService.getPmsProductById(id);
        return pmsProduct;
    }

}
